import Vue from 'vue'
import VueRouter from 'vue-router'
import loginView from "@/login/loginView.vue"
import registerView from "@/components/registerView.vue";
import LayoutView from "@/page/servePage/LayoutView.vue"
import Mall from "@/page/servePage/Views/MailView.vue"
import User from "@/page/servePage/Views/UserView.vue"
import PageOne from "@/page/servePage/Views/PageOne.vue"
import PageTwo from "@/page/servePage/Views/PageTwo.vue"
import Home from "@/page/servePage/Views/HomeView.vue"
Vue.use(VueRouter)

const routes = [
  {
    path:'/server',
    name:'server',
    component: LayoutView,
    children:[
      // 子路由
      // 这是本次写的部分
      { path: '/Home', name: 'Home', component: Home }, // 首页
      { path: '/user', name: 'user', component: User }, // 用户管理
      { path: '/mall', name: 'mall', component: Mall }, // 商品管理
      { path: '/page1', name: 'page1', component: PageOne }, // 页面1
      { path: '/page2', name: 'page2', component: PageTwo }, // 页面2
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: loginView
  },
  {
    path: '/register',
    name: 'register',
    component: registerView
  },
  // 其他路由...
  // 确保在路由列表的末尾有一个通配符路由，以捕获所有未定义的路由
  {
    path: '*',
    redirect: '/login' // 或者重定向到其他页面，比如首页
  },

]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // 如果访问的是登录界面则直接放行
  if (to.path === '/login'||to.path==='/register') return next()
  //获取用户页面token信息
  let token = window.sessionStorage.getItem('token')
  //如果token数据为null则跳转到指定路径
  if (!token) {
    alert("login")
    return next("/login")
  }
  next()
})

export default router
